package com.xh.car.controller;

import com.xh.car.bean.Category;
import com.xh.car.service.CategoryService;
import com.xh.car.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/5/7/007
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("")
    public String index() {
        return "category/category-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Category category) {
        return categoryService.getPage(page, limit, category);
    }

    @GetMapping("/edit")
    public String edit(Integer id, Model model) {
        if (id != null) {
            model.addAttribute("category", categoryService.getById(id));
        }
        return "category/category-edit";
    }

    @PostMapping(value = {"/update", "/status"})
    @ResponseBody
    public R update(Category category) {
        categoryService.saveOrUpdate(category);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        categoryService.deleteById(id);
        return R.ok();
    }

    @GetMapping("/select")
    @ResponseBody
    public R select() {
        return R.data(categoryService.getSelect());
    }

}
